<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\site\ResetNextTime;
use App\Enums\Method;
use App\Models\Categorie;
use App\Models\NoticeTemplate;
use App\Models\Site;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;

class SiteController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '网站';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Site);
        $user_id =Admin::user()->id;
        if( $user_id!=1){
            $grid->model()->where(['admin_user_id'=>$user_id]);
        }

        $grid->column('site_id', __('Site id'));
        $grid->column('web_site_url', __('Web site url'));
        $grid->column('site_name', __('Site name'));
        $grid->column('is_verif_return', __('是否验证返回'))->display(function ($is_verif_return){
            if ($is_verif_return ==1){
                return '是';
            }else{
                return '否';
            }
        });
        $grid->column('error_num', __('状态'))->display(function ($error_num){
            if ($error_num ==0){
                return '<p style="color: green ;">网站正常</p>';
            }else{
                return  '<p style="color: red">异常：请求次数'.$error_num.'</p>';
            }
        });
        $grid->column('notice_template.template_name',__('模版名称'));
        $grid->column('updated_at', __('最后更新的时间'));
        $grid->column('next_perform_time', __('下次执行时间'));
        $grid->actions(function ($actions) {
            $actions->add(new ResetNextTime());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Site::findOrFail($id));

        $show->field('site_id', __('Site id'));
        $show->field('admi_user_id', __('Admi user id'));
        $show->field('web_site_url', __('Web site url'));
        $show->field('site_name', __('Site name'));
        $show->field('notice_template_id', __('Notice template id'));
        $show->field('is_verif_return', __('Is verif return'));
        $show->field('return_content', __('Return content'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Site);

        $form->text('site_name', __('网站名字'));
        $form->text('web_site_url', __('你要监控的网站地址'));
        $form->select('method','方法')->options(Method::All)->default('GET')->required();
        $form->select('notice_template_id',__('模版'))->options(
            NoticeTemplate::where(['admin_user_id'=>Admin::user()->id])->pluck('template_name', 'notice_template_id')
       )->help('模版请在我的模版添加')->required();
        $form->hidden('admin_user_id','')->default(Admin::user()->id);
        $form->switch('is_verif_return', __('是否验证返回'))->help('开启验证返回内容');
        $form->textarea('return_content', __('返回内容'))->help('开启验证返回时对比');

        return $form;
    }
}
